import Vue from 'vue'
import Router from 'vue-router'
import Home from '@/views/Home/Home.vue'

const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
	return originalPush.call(this, location).catch(err => err)
}

Vue.use(Router)

const router = new Router({
	routes: [
		{
			name: 'login',
			path: '/login',
			component: () => import('@/views/Login/Login.vue')
		},
		{
			name: 'home',
			path: '/home',
			component: () => import('@/views/Home/Home.vue'),
			redirect: '/index',
			children: [
				{
					name: 'index',
					path: '/index',
					component: () => import('@/views/Index/Index.vue')
				},
				// 角色列表
				{
					name: 'roles',
					path: '/roles',
					component: () => import('@/views/Roles/Roles.vue')
				},
				// 权限列表
				{
					name: 'rights',
					path: '/rights',
					component: () => import('@/views/Roles/Rights.vue')
				},
				// 基础题库
				{
					name: 'questions',
					path: '/questions',
					component: () => import('@/views/Questions/Questions.vue')
				},
				// 试题录入
				{
					path: '/questions/add/:id?',
					name: 'questionsAdd',
					component: () => import('@/views/Questions/Add.vue'),
					props: true
				},
				// 学科列表
				{
					name: 'subject',
					path: '/subject',
					component: () => import('@/views/Subject/Subject.vue')
				},
				// 学科标签
				{
					name: 'tags',
					path: '/tags',
					component: () => import('@/views/Subject/Tags.vue')
				},
				// 文章列表
				{
					name: 'articles',
					path: '/articles',
					component: () => import('@/views/Articles/Articles.vue')
				},
				// 新增技巧
				{
					name: 'articlesAdd',
					path: '/articles/add',
					component: () => import('@/views/Articles/Add.vue')
				}
			]
		}
	]
})

router.beforeEach((to, from, next) => {
	if (to.path == '/login') return next()

	const token = window.localStorage.getItem('token')

	token ? next() : next('/login')
})

export default router
